<?php

class Widget_ProductSearch extends WP_Widget {

	function __construct() {
		$widget_ops = array('classname' => 'widget_productsearch', 'description' => __( "Tìm kiếm sản phẩm") );
		parent::WP_Widget( false, __( 'Tìm kiếm sản phẩm', 'woothemes' ), $widget_ops );
	}

	function widget( $args, $instance ) {
		extract($args);
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
        
        
		echo $before_widget;
		if ( $title )
			echo $before_title . $title . $after_title;

		// Use current theme search form if it exists
		?>
        <form class="form-search" action="<?php echo esc_url( home_url( '/' ) );?>" method="get">
            <input type="hidden" name="search" value="product" />
            <input type="text" name="s" value="<?php echo get_search_query();?>" />
            <input type="submit" value="Tìm" class="btn" />
        </form>
        <?php
        
		echo $after_widget;
        ?>
       <script type='text/javascript'>
        /* <![CDATA[ */
        	jQuery(function ( $ ){
        	   var icon = $("#<?php echo $widget_id;?>").find(".sidebar-title").children(".icon");
               icon.removeClass("icon-star").addClass("icon-search");
               $(".form-search").submit(function(){
                    if($(this).find("input[name='s']").val()==""){
                        alert("Nhập từ khóa tìm kiếm");
                        return false;
                    }
               });
        	});
        /* ]]> */
        </script>
       <?php 
        
	}

	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
		$title = $instance['title'];
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
<?php
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
		$instance['title'] = strip_tags($new_instance['title']);
		return $instance;
	}

}


register_widget( 'Widget_ProductSearch' );
?>